var schoolTreeSetting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    },
    callback: {
        onClick: chooseSchool
    }
};
var schoolTree;

function del(item) {
    var ids = item;
    if(ids == null){
        return ;
    }

    confirm('确定要删除选中的记录？', function() {
        $.ajax({
            type: "GET",
            url: baseURL + "school/profession/delete?id="+ids,
            success: function (r) {
                if (r.code == 0) {
                    alert('操作成功');
                    $("#jqGrid").trigger("reloadGrid");

                } else {
                    alert(r.msg);
                }

            }
        });
    });
}


function chooseSchool(event, treeId, treeNode) {
    vm.school.id=treeNode.id
    vm.school.name=treeNode.name
    vm.schoolName=treeNode.name
    vm.profession={id:null,code:null,name:null,type:'普通', wenlike:""}
    vm.reload()
}

function updateWenkeScore(id,courseId,studentId) {
    //alert(id)
    $(`#wenke${id}`).html(`<input id="inputWenke${id}" type="number" onchange="updateWenke(${id})" />`);
}

function updateWenke(id) {
   var score= $(`#inputWenke${id}`).val()
    $.ajax({
        type: "GET",
        url: baseURL + `school/profession/updateLowScore?id=${id}&wenkeScore=${score}`,
        success: function (r) {
            if (r.code == 0) {
                vm.reload()
            } else {
                alert(r.msg);
            }

        }
    });



}

function updatelikeScore(id,courseId,studentId) {
    //alert(id)
    $(`#like${id}`).html(`<input id="inputlike${id}" type="number" onchange="updateLike(${id})" />`);
}
function updateLike(id) {
    var score= $(`#inputlike${id}`).val()
    $.ajax({
        type: "GET",
        url: baseURL + `school/profession/updateLowScore?id=${id}&likeScore=${score}`,
        success: function (r) {
            if (r.code == 0) {
                vm.reload()
            } else {
                alert(r.msg);
            }

        }
    });



    vm.reload()
}

$(function () {
    vm.getSchoolTree()
    $("#jqGrid").jqGrid({
        url: baseURL + 'school/profession/list',
        datatype: "json",
        colModel: [
            { label: '专业代码', name: 'code', width: 40 },
            { label: '专业名称', name: 'name', width: 120 },
            { label: '文理科', name: 'wenlike', width: 40 },
            { label: '专业类型', name: 'type', width: 40 },
            { label: '文科预估最低分', name: 'wenkeScore', width: 40 ,
                formatter: function (data,index,row) {
                    var re;

                    if(data==null)
                        re=`<div id="wenke${index.rowId}" ><label onclick="updateWenkeScore(${index.rowId})">未录入</label></div>`
                    else
                        re= `<div id="wenke${index.rowId}"><label onclick="updateWenkeScore(${index.rowId})">${data}</label></div>`;
                    return re;

                }},
            { label: '文科理科最低分', name: 'likeScore', width: 40,
                formatter: function (data,index,row) {
                    var re;

                    if(data==null)
                        re=`<div id="like${index.rowId}" ><label onclick="updatelikeScore(${index.rowId})">未录入</label></div>`
                    else
                        re= `<div id="like${index.rowId}"><label onclick="updatelikeScore(${index.rowId})">${data}</label></div>`;
                    return re;

                } },
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        },
        onCellSelect:function (id,code,name,b,a) {
            $.ajax({
                type: "GET",
                url: baseURL + "school/profession/info/"+id,
                success: function (r) {
                    if (r.code == 0) {
                        vm.profession=r.profession
                        vm.reloadProfessional()
                        console.log(r)

                    } else {
                        alert(r.msg);
                    }

                }
            });



        }
    });
})


var vm = new Vue({
    el:'#rrapp',
    data:{
        school:{id:0,name:null},
        profession:{id:null,code:null,name:null,type:'普通', wenlike:""},
        schoolName:null,
        types:["普通","校企合作","中外合作","艺术","春考","3+2连读"],
        wenlike:["文科","理科","文理兼收"],
        showList:true,
        showYearList:true,
        title:null,
        noHasSchoolYear:["2018","2017","2016"],
        lowscore:{}
    },
    methods: {
        addPYearScore:function () {
            if(vm.profession.id==null){
                alert("请选择专业")
            }else {
                vm.showYearList = false
            }
        },
        courseTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择菜单",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#menuLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = zCoursetree.getSelectedNodes();
                    if(node[0].id<10000) {
                        //选择上级菜单
                        vm.teachPlan.courseId=node[0].id;
                        vm.teachPlan.title = node[0].name;
                        layer.close(index);
                    }
                }
            });
        },
        getSchoolTree: function(menuId){
            //加载学校树
            $.get(baseURL + "school/school/getSchoolTree", function(r){
                schoolTree = $.fn.zTree.init($("#schoolTree"), schoolTreeSetting, r.schoolList);
                var node = schoolTree.getNodeByParam("id", 0);
                schoolTree.selectNode(node);
            })



        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'schoolId': vm.school.id},
                page:page
            }).trigger("reloadGrid");
        },
        reloadProfessional: function (event) {
            vm.showList = true;
            var page = $("#jqGridProfessional").jqGrid('getGridParam','page');
            $("#jqGridProfessional").jqGrid('setGridParam',{
                postData:{
                    'schoolId': vm.school.id,
                    'professionId': vm.profession.id
                },
                page:page
            }).trigger("reloadGrid");
        },
        add: function(){
            if(vm.school.id<10){
                alert("请先选择学校")
            }else {
                vm.showList = false;
                vm.title = "新增";
                vm.lowscore = {year: null,schoolId:vm.school.id}
            }
        },
        saveOrUpdate:function () {
                var url = "school/profession/save";
                vm.profession.schoolId=vm.school.id
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.profession),
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(index){
                                vm.reload();
                                //   location. reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
        },
        saveYear:function () {
            vm.showYearList=true
            vm.professionLowScore.schoolId=vm.school.id
            vm.professionLowScore.schoolName=vm.school.name
            vm.professionLowScore.professionId=vm.profession.id
            vm.professionLowScore.professionName=vm.profession.name
            vm.professionLowScore.professionCode=vm.profession.code
            vm.professionLowScore.professionType=vm.profession.type
            if(vm.professionLowScore.year==null){
                alert("年份有误")

            }else{
                var url = "school/profession/saveProfessionYear";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.professionLowScore),
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(index){
                                vm.reloadProfessional();
                                //   location. reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            }


        },
        reloadYear:function () {
            vm.showYearList=true
        }

    }

});